package com.zust.data.web.rest;

import com.zust.data.domain.param.DataSaveParam;
import com.zust.data.dto.ResponseDto;
import com.zust.data.dto.SessionInfo;
import com.zust.data.entity.DlData;
import com.zust.data.serivce.ColumnService;
import com.zust.data.serivce.DataService;
import com.zust.data.util.StrUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author senselyan
 * @Description:
 * @date 2018/5/10 17:02
 */
@RestController
@RequestMapping("/data")
public class DataResource {

    @Inject
    private DataService dataService;
    @Inject
    private ColumnService columnService;

    @RequestMapping("/doSave")
    public ResponseDto save(DataSaveParam dataSaveParam, HttpServletRequest request, HttpSession session) {
        SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
        dataSaveParam.setUserId(sessionInfo.getId());
        return dataService.save(dataSaveParam);
    }

    @RequestMapping("/{dataId}/changeName")
    public ResponseDto save(@PathVariable String dataId,
                            @RequestParam(value = "type", required = true) Integer type,
                            @RequestParam(value = "value", required = true) String value) {
        dataService.changeName(dataId, type, value);
        return new ResponseDto(true, "操作成功");
    }


    @RequestMapping("/{dataId}/addColumn")
    public ResponseDto addColumn(@PathVariable String dataId,
                                 @RequestParam(value = "num", required = false) Integer num) {
        if (ObjectUtils.isEmpty(num)) {
            num = 1;
        }
        return columnService.addEmptys(dataId, num);
    }

    @RequestMapping("/{dataId}/delete")
    public ResponseDto deleted(@PathVariable String dataId) {
        dataService.deleted(dataId);
        return new ResponseDto(true, "操作成功");
    }

//    @RequestMapping("/{dataId}/column/{columnId}/save")
//    public ResponseDto saveColumn(@PathVariable String dataId, @PathVariable String columnId)

    //localhost:8080/data/5af948319e0e290d78cc8f68/deleteColumn
    @RequestMapping("/{dataId}/deleteColumn")
    public String deleteColumn(@PathVariable String dataId) {
        columnService.delete(dataId);
        return "";
    }

}
